<template>
	<a-modal
		title="消息提醒"
		:width="500"
		:visible="visible"
		:destroy-on-close="true"
		@cancel="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="消息类型：" name="msgType">
				<a-select v-model:value="formData.msgType" :options="msgTypeOptions" placeholder="请选择消息类型"/>
			</a-form-item>
			<a-form-item label="发送方式" name="sendType">
				<a-select v-model:value="formData.sendType" :options="sendTypeOptions" placeholder="请选择发送方式"/>
			</a-form-item>
			<a-form-item v-if="formData.sendType === 'MP'" label="公众号">
				<a-select v-model:value="formData.tenantId" :options="tenantMpOptions" placeholder="请选择公众号"/>
			</a-form-item>
			<a-form-item v-if="formData.msgType === ''" label="模板ID：" name="templateId">
				<a-input v-model:value="formData.templateId" placeholder="请输入微信公众号模板消息ID"/>
			</a-form-item>
			<a-form-item label="消息内容：" name="content">
				<a-textarea type="textarea" v-model:value="formData.note" />
			</a-form-item>
			<a-form-item label="跳转链接" name="link">
				<a-input allowClear v-model:value="formData.link" placeholder="请输入H5链接"/>
			</a-form-item>
			<a-form-item label="跳转小程序路径" name="appPath">
				<a-input allowClear v-model:value="formData.appPath" placeholder="请输入小程序路径"/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</a-modal>
</template>

<script setup>
	import { message } from 'ant-design-vue'
	import {required} from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import crmApi from '@/api/crm/crmApi'
	import { getCurrentInstance } from 'vue'
	import tool from "@/utils/tool";
	// 默认是关闭状态
	let visible = $ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const iconSelector = ref()
	// 打开抽屉
	const onOpen = (record) => {
		visible = true
		formData.value = {}
		if (record) {
			formData.value = Object.assign({}, record)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible = false
	}

	// 默认要校验的
	const formRules = {
		msgType: [required('请选择消息类型')],
		sendType: [required('请选择操作类型')],
		recharge: [required('请输入本金，精确到小数点后两位')],
		present: [required('请输入赠送金，精确到小数点后两位')]
	}



	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.icon = value
	}
	const { proxy } = getCurrentInstance()

	let msgTypeOptions = tool
		.dictTypeList('MSG_TYPE')
		.map((item) => {
			return {
				value: item['id'],
				label: item['name']
			}
		})

	let sendTypeOptions = tool
		.dictTypeList('SEND_TYPE')
		.map((item) => {
			return {
				value: item['id'],
				label: item['name']
			}
		})

	let tenantMpOptions = tool
		.tableList("TENANT_MP")

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				const param = Object.assign({}, formData.value)
				submitLoading.value = true
				crmApi.sendMesage(param).then(() => {
					visible = false
					emit('successful')
					message.success('发送成功')
				}).finally(() => {
					submitLoading.value = false
				})
			})
	}
	const parameterChanges = (data) => {
		return data
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
